package com.toobigtofail.posevo
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Graphic;
	import net.flashpunk.World;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	public class PlanetOfSubarus extends World
	{
		private var _player:PontiacOnSteroids;
		private var _roadLines:Array = new Array;
		private var _road:PassOnSurface;
		
		
		private var _velocityX:Number = 0;
		public var velocityY:Number = 1;
		private var _distance:Number = 0;
		private var _lastDraw:Number = 0;
		
		private static const LINE_SPACING:Number = 200;
		
		private static const VELOCITY_X_INCREMENT:Number = 5;
		private static const VELOCITY_Y_INCREMENT:Number = 3
		private static const DRAG:Number = .95; //Should be 1 > DRAG > .7ish  //Lower seems more precise
		private static const MAX_Y:Number = 400;
		private static const MAX_X:Number = 30;
		//private static const SCENERY_TIMER = 3;
		
		public function PlanetOfSubarus()
		{
			super();
		}
		
		override public function begin():void
		{
			_player = new PontiacOnSteroids(FP.screen.width/2, 3*FP.screen.height/4);
			add(_player);
			
		}
		
		override public function update():void
		{
			handleInputs();
			updatePlayer();
			drawRoad();
			
			
			super.update();
		}
		
		private function handleInputs():void
		{
			//handle inputs
			if(Input.check(Key.LEFT))
			{
				_velocityX-=VELOCITY_X_INCREMENT;
			}
			if(Input.check(Key.RIGHT))
			{
				_velocityX+=VELOCITY_X_INCREMENT;
			}
			if (_velocityX > MAX_X)
				_velocityX = MAX_X;
			if (_velocityX < -MAX_X)
				_velocityX = -MAX_X;
		}
		
		private function updatePlayer():void
		{
			_player.x += _velocityX;
			_velocityX *= DRAG;
			velocityY += velocityY*.01*FP.elapsed;
			if (velocityY > MAX_Y)
				velocityY = MAX_Y
			_distance += velocityY;
			
		}
		
		private function drawRoad():void
		{
			if(_distance > _lastDraw + LINE_SPACING)
			{
				_lastDraw = _distance;
				_roadLines.push(this.add(new Entity(FP.screen.width/2, -20, Image.createRect(4,50,0x164C89))));
			}
			
			for each (var rl:Entity in _roadLines)
			{
				rl.y += velocityY;
				if (rl.y > FP.screen.height)
					remove(rl);
					
				
			}
			
		}
	}
}